﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockCommon;
using Utilities;

namespace StockImport.TaskLog
{
    public class UpdateScheduleNaviServices
    {

        static Dictionary<String, int> categoryToIds = null;

        protected  class catids
        {
            public String category {get;set;}
            public int label { get; set; }
        }
        protected static void initCategoryToIds()
        {
            StockDataClassesDataContext context=getContext();
            var cats=context.ExecuteQuery<catids>("select distinct category , label from ScheduleLogX");
            if(cats!=null)
            {
                foreach(catids c in cats)
                {
                    categoryToIds.Add(c.category,c.label);
                }
            }
        }

        private static StockDataClassesDataContext getContext()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            return context;
        }
        protected static int getLabel(String category)
        {
            if(categoryToIds==null)
                initCategoryToIds();
            if (categoryToIds.ContainsKey(category))
            {
            }
            return 1;


        }
        public static void UpdateSBDetail()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            DateTime dts = context.ExecuteQuery<DateTime>("select MIN(日期) as 日期 from SBDetails").FirstOrDefault<DateTime>();
            if (dts == null)
                return;
            DateTime targetDate = DateUtils.getDateOnly(DateTime.Now);

            while (targetDate.CompareTo(dts) >= 0)
            {
                UpdateSBDetail(targetDate);
                targetDate = targetDate.AddDays(-1);
            }
        }


        public static bool Update_TseCount()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            List<TT> ll = context.ExecuteQuery<TT>("select COUNT(*) as c,StockDaily.日期 from StockDaily  with (NOLOCK) where StockDaily.證券代號 in (select 代號 from StockIndex with (NOLOCK) where StockIndex.市場='tse') group by StockDaily.日期 order by  StockDaily.日期").ToList<TT>();
            foreach(TT tt in ll)
            {
             UpdateScheduleNaviServices.logItem(tt.日期, "上市_股票行情", tt.c.ToString() + "筆", 1);
            }
            return true;
            
        }
        public class TT
        {
            public int c;
            public DateTime 日期;
        }
        public static bool Update_OtcCount()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            List<TT> ll = context.ExecuteQuery<TT>("select COUNT(*) as c,StockDaily.日期 from StockDaily  with (NOLOCK) where StockDaily.證券代號 in (select 代號 from StockIndex with (NOLOCK) where StockIndex.市場='otc') group by StockDaily.日期 order by  StockDaily.日期").ToList<TT>();
            foreach(TT tt in ll)
            {
             UpdateScheduleNaviServices.logItem(tt.日期, "上櫃_股票行情", tt.c.ToString() + "筆", 2);
            }
            return true;
        }
        public static bool UpdateSBDetail(DateTime date)
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            int c = context.ExecuteQuery<String>("select distinct StockCode from SBDetails with (NOLOCK) where 日期= {0}", date).Count();
            if (c > 0)
            {
                return UpdateScheduleNaviServices.logItem(date, "上市買賣明細", c.ToString() + "家", 3);
            }
            return false;

        }

        public static bool logItem(DateTime date, String type, String msg, int label)
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            ScheduleLogX item = (from s in context.ScheduleLogXes where s.StartDate == date && s.Subject.StartsWith(type) select s).FirstOrDefault<ScheduleLogX>();
            System.Diagnostics.Debug.WriteLine(date.ToShortDateString() + ":" + type + "\t" + msg);
            if (item != null)
            {
                String newsub = type + ":" + msg;
                if ((newsub.Equals(item.Subject)) &&
                    (label == item.Label))
                {
                    return true;
                }
                item.Subject = newsub;
                item.Label = label;
                context.SubmitChanges();
                return true;
            }
            item = new ScheduleLogX();
            item.Type = 0;
            item.StartDate = date;
            item.EndDate = date;
            item.AllDay = true;
            item.Subject = type + ":" + msg;
            item.Location = "";
            item.Description = "";
            item.Status = 0;
            item.Label = label;
            item.ReminderInfo = "";
            item.percentInfo = 0;
            item.category = type;
            context.ScheduleLogXes.InsertOnSubmit(item);
            context.SubmitChanges();
            return false;
        }


    }
}
